package com.lzw.mall.admin.util;

import com.lzw.mall.admin.entity.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class RequestUtil {
    private final static String CURRENT = "currentUser";

    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }

    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    //保存当前登录用户进session
    public static void setCurrent(User user) {
        getSession().setAttribute(CURRENT, user);
    }


    //获取当前session中保存的登录用户对象
    public static User getCurrent() {

        Object obj = getSession().getAttribute(CURRENT);
        if (obj == null) {
            return null;
        }
        return (User) obj;
    }


    public static void deleteCurrent() {
        getRequest().getSession().removeAttribute(CURRENT);
    }

}
